import history from '@/utils/history'
import { hasToken } from '@/utils/storage'
import { Redirect, Route } from 'react-router'

// 预期  <AnthRoute path="/profile/chat" component={ProfileChat} />

type Props = {
  path: string
  component: any
}

export default function AnthRoute({ path, component: Component }: Props) {
  return (
    <Route
      path={path}
      render={() => {
        if (hasToken()) {
          // 有 token 正常渲染组件
          return <Component />
        } else {
          // 没有 token 重定向登录页
          return (
            <Redirect
              to={{
                pathname: '/login',
                state: { from: history.location.pathname },
              }}
            />
          )
        }
      }}
    />
  )
}
